﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using CDMS.Common;
using CDMS.Entities;
using DevExpress.LookAndFeel;
using DevExpress.XtraSplashScreen;
using CDMS.BLL;

namespace CDMS
{
    public partial class ucCateogryList : DevExpress.XtraEditors.XtraUserControl
    {
        Category_BLL category_BLL;
        EnumCategory enumCategory;
        int iD = 0;
        string caption = "";
        public ucCateogryList(EnumCategory enumCategory, string caption)
        {
            InitializeComponent();
            this.layoutControl1.AllowCustomizationMenu = false;
            this.enumCategory = enumCategory;
            this.caption = caption;
            category_BLL = new Category_BLL(enumCategory);

            
            AddColumn();
            LoadData();
        }

        private void AddColumn()
        {
            gridView1.Columns.Clear();

            //C1:

            //string[] fields = { "Users_ID", "Users_Name", "Users_Note", "Users_Activated" };
            //string[] captions = { "", "Tên đăng nhập", "Ghi chú", "", "" };
            //for (int i = 0; i < fields.Length; i++)
            //{
            //    DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
            //    col.FieldName = fields[i];
            //    col.Caption = captions[i];
            //    gridView1.Columns.Add(col);
            //    gridView1.Columns[i].Visible = true;
            //}
            //gridView1.Columns["Users_ID"].Visible = false;
            //gridView1.Columns["Users_Activated"].Visible = false;
            //gridView1.Columns["Users_Name"].Width = 200;

            //C2:
            switch (enumCategory)
            {
                case EnumCategory.ProductGroup:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ProductGroup_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ProductGroup_Name", "Tên nhóm", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ProductGroup_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ProductGroup_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.Unit:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Unit_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Unit_Name", "Tên đơn vị tính", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Unit_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Unit_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.UserGroup:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "UserGroup_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "UserGroup_Name", "Tên nhóm", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "UserGroup_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "UserGroup_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.Users:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Users_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Users_Name", "Tên đăng nhập", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Users_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Users_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.Position:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Position_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Position_Name", "Tên vị trí", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Position_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Position_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.Vocative:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Vocative_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Vocative_Name", "Tên xưng hô", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Vocative_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Vocative_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.CustomerGroup:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerGroup_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerGroup_Name", "Tên nhóm", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerGroup_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerGroup_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.CustomerType:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerType_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerType_Name", "Tên loại", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerType_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerType_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.MarketSegment:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MarketSegment_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MarketSegment_Name", "Tên phân khúc", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MarketSegment_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MarketSegment_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.SourceInfo:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SourceInfo_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SourceInfo_Name", "Tên nguồn", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SourceInfo_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SourceInfo_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.Nationlaty:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Nationlaty_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Nationlaty_Name", "Tên quốc tịch", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Nationlaty_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Nationlaty_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.City:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "City_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "City_Name", "Tỉnh, thành phố", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "City_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "City_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.MemberType:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MemberType_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MemberType_Name", "Tên thành viên", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MemberType_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MemberType_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.ContractType:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractType_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractType_Name", "Tên loại", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractType_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractType_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.ContractStatus:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractStatus_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractStatus_Name", "Tên trạng thái", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractStatus_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractStatus_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.QuotationStatus:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "QuotationStatus_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "QuotationStatus_Name", "Tên trạng thái", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "QuotationStatus_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "QuotationStatus_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.OrderStatus:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "OrderStatus_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "OrderStatus_Name", "Tên trạng thái", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "OrderStatus_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "OrderStatus_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.VATCode:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "VATCode_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "VATCode_Name", "Mã thuế", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "VATCode_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "VATCode_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.SalesArea:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SalesArea_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SalesArea_Name", "Tên điểm bán", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SalesArea_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SalesArea_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.IncomeDocType:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "IncomeDocType_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "IncomeDocType_Name", "Tên loại", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "IncomeDocType_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "IncomeDocType_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.SpendDocType:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SpendDocType_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SpendDocType_Name", "Tên loại", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SpendDocType_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SpendDocType_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.LimitDebitAge:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "LimitDebitAge_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "LimitDebitAge_Name", "Tên", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "LimitDebitAge_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "LimitDebitAge_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.PaymentWay:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PaymentWay_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PaymentWay_Name", "Tên", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PaymentWay_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PaymentWay_Activated", "Hoạt động", "", 0, true, true);
                    break;
                case EnumCategory.PriceType:
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PriceType_ID", "", "", 0, false, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PriceType_Name", "Tên loại", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PriceType_Note", "Ghi chú", "", 200, true, true);
                    FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "PriceType_Activated", "Hoạt động", "", 0, true, true);
                    break;
                default:
                    break;
            }

        }


        void LoadData()
        {
            //Open Wait Form
            FormGlobals.ShowWaitForm();
            try
            {
                DataTable dt = new DataTable();
                dt = category_BLL.GetALLActivated();
                gridControl1.DataSource = dt;
                gridControl1.ForceInitialize();
            }
            finally
            {
                //Close Wait Form
                FormGlobals.CloseWaitForm();
            }
        }

        private void barButtonItemAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            Forms.frmCategoryInfo f = new Forms.frmCategoryInfo(enumCategory, 0);

            if (f.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void barButtonItemEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;

            iD = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);
            Forms.frmCategoryInfo f = new Forms.frmCategoryInfo(enumCategory, iD);

            if (f.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }


        private void barButtonItemDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;

            if (FormGlobals.Message_QuestionYesNo("Bạn có chắc chắn muốn xóa dòng này?", "Xác nhận xóa") == DialogResult.Yes)
            {

                iD = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);

                if (category_BLL.Delete(iD))
                    LoadData();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            FormGlobals.ExportDataToExcel(gridControl1, caption);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            barButtonItemEdit.PerformClick();
        }


    }
}
